package com.cloudxingkong.dianyinke.NewTeacher.Utils;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cloudxingkong.dianyinke.NewTeacher.Ui.SettingsActivity;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.io.File;
import java.io.FilenameFilter;


public class HomePicAsyncTask extends AsyncTask<String, Void, byte[]> {
	private Context context;
	private String fileName;
	private File []homePic;
	private RollPagerView rollPagerView;

	public HomePicAsyncTask(Context context, String fileName, RollPagerView rollPagerView) {
		this.context = context;
		this.fileName = fileName;
		this.rollPagerView = rollPagerView;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected byte[] doInBackground(String... params) {
		return HttpUtils.request(params[0]);
	}
	@Override
	protected void onPostExecute(byte[] result) {
		super.onPostExecute(result);
		if (result!=null) {
			boolean dianYinKe = SDCardHelper.saveFileToSDCard(result, "DianYinKe", fileName);


			if (dianYinKe) {
				Log.e("test","保存主页图片成功");
				File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DianYinKe/");
				boolean exists = file.exists();
				try {
					homePic=file.listFiles(new FilenameFilter() {
						@Override
						public boolean accept(File dir, String filename) {
							if (filename.contains(".jpg")||filename.contains(".JPG")||filename.contains(".png")
									||filename.contains(".PNG")||filename.contains(".jpeg")||filename.contains(".JPEG")
									||filename.contains(".bmp")||filename.contains(".BMP")){
								return true;
							}
							return false;
						}
					});
				}catch (Exception e){
					Log.e("test","HomePicAsyncTask Error : "+e.getMessage());
				}finally {
					int length = homePic.length;

					PicLoopAdapter loopAdapter = new PicLoopAdapter(rollPagerView,homePic,context);
					rollPagerView.setAdapter(loopAdapter);
					rollPagerView.setHintView(new ColorPointHintView(context, Color.parseColor("#0F9E59"), Color.WHITE));
					loopAdapter.notifyDataSetChanged();
				}


			}else{
				Log.e("test", "保存主页图片失败");
			}
		}


	}



	private class PicLoopAdapter extends LoopPagerAdapter {
		private File [] homePic;
		private Context context;

		public PicLoopAdapter(RollPagerView viewPager, File[] homePic, Context context) {
			super(viewPager);
			this.homePic = homePic;
			this.context = context;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			
			return super.instantiateItem(container, position);
		}

		@Override
		public View getView(ViewGroup container, int position) {
			PhotoUtils utils=new PhotoUtils();
			PhotoUtils.CompressOptions options=new PhotoUtils.CompressOptions();
			options.uri= Uri.fromFile(new File(String.valueOf(homePic[position])));
			Bitmap bitmap = utils.compressFromUri(context, options);

			ImageView view = new ImageView(container.getContext());
			view.setImageBitmap(bitmap);
			view.setScaleType(ImageView.ScaleType.CENTER_CROP);
			view.setClickable(true);
			view.setFocusable(true);
			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					context.startActivity(new Intent(context, SettingsActivity.class));
				}
			});
			view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

			return view;
		}

		@Override
		protected int getRealCount() {
			return homePic.length;
		}
	}
}
